
#include <librally.h>

Err rp_cond_init(rp_cond_t * cond)
{
	static char fname[] = "rp_cond_init";
	static char msg [] = "fatal error";
	Err err;
	int ret;
	FLOG_IN(fname);

	ret = pthread_cond_init(cond, NULL);
	if (ret)
	{
		rp_log(LOG_CRITICAL, fname, "Cannot initialize the conditional variable. Erro: %d", ret);
		return E_ERROR;
	}
	
	FLOG_OUT(fname);
	return E_OK;
}

Err rp_cond_destroy(rp_cond_t *cond)
{
	static char fname[] = "rp_cond_destroy";
	static char msg [] = "fatal error";
	Err err;
	int ret;
	FLOG_IN(fname);

	ret = pthread_cond_destroy(cond);
	if (ret)
	{
		rp_log(LOG_CRITICAL, fname, "Cannot destroy the conditional variable. Erro: %d", ret);
		return E_ERROR;
	}
	
	FLOG_OUT(fname);
	return E_OK;
}

Err rp_cond_wait(rp_cond_t * cond, rp_mutex_t * mutex)
{
	static char fname[] = "rp_cond_wait";
	static char msg [] = "fatal error";
	Err err;
	int ret;
	FLOG_IN(fname);

	ret = pthread_cond_wait(cond, mutex);
	if (ret)
	{
		rp_log(LOG_CRITICAL, fname, "Cannot wait for the conditional variable. Erro: %d", ret);
		return E_ERROR;
	}
	
	FLOG_OUT(fname);
	return E_OK;
}

Err rp_cond_signal(rp_cond_t * cond)
{
	static char fname[] = "rp_cond_singal";
	static char msg [] = "fatal error";
	Err err;
	int ret;
	FLOG_IN(fname);

	ret = pthread_cond_signal(cond);
	if (ret)
	{
		rp_log(LOG_CRITICAL, fname, "Cannot signal to the conditional variable. Erro: %d", ret);
		return E_ERROR;
	}

	FLOG_OUT(fname);
	return E_OK;	
}

Err rp_cond_broadcast(rp_cond_t * cond)
{
	static char fname[] = "rp_cond_broadcast";
	static char msg [] = "fatal error";
	Err err;
	int ret;
	FLOG_IN(fname);

	ret = pthread_cond_broadcast(cond);
	if (ret)
	{
		rp_log(LOG_CRITICAL, fname, "Cannot broadcast to the conditional variable. Erro: %d", ret);
		return E_ERROR;
	}
	
	FLOG_OUT(fname);
	return E_OK;	
}

